package com.qr.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qr.pojo.User;
import com.qr.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/User")
public class UserController {
  @Autowired
    private UserService userService;

    @RequestMapping("/allUser")
    public String list(Model usermodel) {
        List<User> list = userService.AllUser();
        usermodel.addAttribute("list", list);
        return "allUser";
    }
//
//    @RequestMapping("allUser")
//    public String userList(@RequestParam(value="page",defaultValue="1") int page,
//                           Model model){
//        PageHelper.startPage(page,5);
//        List<User> list=userService.AllUser();
//        PageInfo<User> p=new PageInfo<User>(list);
//        model.addAttribute("list",list);
//        model.addAttribute("page",p);
//        return "allUser";
//    }

    @RequestMapping("/toAddUser")
    public String toaddUser(){
        return "addUser";
    }

    @RequestMapping("/addUser")
    public String addUser(User user,Model model){
        userService.insertUser(user);
        List<User> list = userService.AllUser();
        model.addAttribute("list", list);
        return "allUser";
    }
    @RequestMapping("/deleteUser")
    public String todelete(String username,Model model){
        userService.deleteUser(username);
        List<User> list = userService.AllUser();
        model.addAttribute("list", list);
        return "allUser";
    }
    @RequestMapping("/toUpdate")
    public String toupdateuser(String username, Model model){
        User user = userService.findUserByName(username);
        model.addAttribute("getuser",user);
        return "updateUser";
    }
    @RequestMapping("/updateUser")
    public String doUpdate(User user,Model model){
        userService.updateUser(user);
        List<User> list = userService.AllUser();
        model.addAttribute("list", list);
        return "allUser";
    }

    @RequestMapping("/tologin")
    public String toLogin() {
        return "login";
    }

    @RequestMapping("/login")
    public String login(HttpServletRequest request, Model mod) {
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        if (username != null && password != null) {
            User user = userService.findUserByName(username);
            if (user != null) {
                if (!user.getPassword().equals(password)) {
                   mod.addAttribute("msg", "密码错误,请重试");
                    return "login";
                } else {
                    return "shucheng";
                }
            } else {
             mod.addAttribute("msg", "未查到相关用户信息，请先进行注册");
                return "regist";
            }
        }
        return "login";

    }

    @RequestMapping("/toregist")
    public String toregist(){
        return "regist";
    }

    @RequestMapping("regist")
    public String resiger(HttpServletRequest request, ModelMap map) {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        if (username != null && password != null) {
            User user = userService.findUserByName(username);
            if (user != null) {
                if (!user.getUsername().equals(username)) {
                    map.addAttribute("msg", "用户名重复，请更换");
                    return "regist";
                } else {
                    return "shucheng";
                }
            } else {
                map.addAttribute("msg", "未查到相关用户信息，请先进行注册");
                return "regist";
            }
        }
        return"regist";
    }
    @RequestMapping("/shucheng")
    public String toshucheng(){
        return "shucheng";
    }



}